package com.letu.uid.baidu.rule;

import com.letu.core.context.ContextUtil;
import com.letu.uid.baidu.impl.CachedUidGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Base64;

/**
 * 雪花id规则
 */
@Component
public class SnowFlakeRole {

    @Autowired
    CachedUidGenerator cachedUidGenerator;

    /**
     * 简单返回雪花ID
     *
     * @return
     */
    public long findUidSimple() {
        return cachedUidGenerator.getUID();
    }

    /**
     * 雪花ID归属用户
     *
     * @param userId
     * @param dbSuffix    库后缀，跟sharding配置一致
     * @param tableSuffix 表后缀，跟sharding配置一致
     * @return
     */
    public long findUidOfUser(Long userId, Integer dbSuffix, Integer tableSuffix) {
        if (userId == null) {
            return findUidSimple();
        }
        return findId(userId, dbSuffix, tableSuffix);
    }


    /**
     * 雪花ID归属用户
     *
     * @param dbSuffix    库后缀，跟sharding配置一致
     * @param tableSuffix 表后缀，跟sharding配置一致
     * @return
     */
    public long findUidOfUser(Integer dbSuffix, Integer tableSuffix) {
        return findUidOfUser(ContextUtil.getUserId(), dbSuffix, tableSuffix);
    }

    /**
     * 目标id
     *
     * @param id
     * @param dbSuffix
     * @param tableSuffix
     * @return
     */
    public long findId(Long id, Integer dbSuffix, Integer tableSuffix) {
        Long dbRemainder = null, tableRemainder = null;
        if (dbSuffix != null) {
            dbRemainder = id % dbSuffix;
        }
        if (tableSuffix != null) {
            tableRemainder = id % tableSuffix;
        }

        long l = System.currentTimeMillis();
        for (; ; ) {
            long newId = cachedUidGenerator.getUID();
            if (dbSuffix != null && tableSuffix != null
                    && dbRemainder != null && tableRemainder != null
                    && (newId % dbSuffix) == dbRemainder && (newId % tableSuffix) == tableRemainder) {
                return newId;
            } else if (dbSuffix != null && dbRemainder != null && (newId % dbSuffix) == dbRemainder) {
                return newId;
            } else if (tableSuffix != null && tableRemainder != null && (newId % tableSuffix) == tableRemainder) {
                return newId;
            } else if (tableSuffix == null && dbSuffix == null) {
                return newId;
            }
        }


    }


    /**
     * 固定库和固定表
     *
     * @param dbSuffix    库前缀
     * @param dbNum       指定库
     * @param tableSuffix 表前缀
     * @param tableNum    指定表
     * @return
     */
    public long findFixedId(Integer dbSuffix, Integer dbNum, Integer tableSuffix, Integer tableNum) {
        for (; ; ) {
            long newId = cachedUidGenerator.getUID();
            if (dbNum != null && tableNum != null && (newId % dbSuffix) == dbNum && (newId % tableSuffix) == tableNum) {
                return newId;
            } else if (dbNum != null && (newId % dbSuffix) == dbNum) {
                return newId;
            } else if (tableNum != null && (newId % tableSuffix) == tableNum) {
                return newId;
            } else if (dbNum == null && tableNum == null) {
                return newId;
            }
        }
    }






    /**
     * 查询库前缀
     *
     * @param dbSuffix    库前缀
     * @param snowId       雪花id
     * @return
     */
    public Integer findDbNum(Integer dbSuffix, Long snowId) {
        Long l = snowId % dbSuffix;
        return l.intValue();
    }
//    public static void main(String[] args) {
//
//        String str= new String(Base64.getDecoder().decode("YWRtaW4="));
//        System.out.println(str);
//
//        System.out.println(new String( Base64.getEncoder().encode("admin".getBytes())));
//    }
}
